﻿#include "schemewindow.h"
#include "ui_schemewindow.h"

SchemeWindow::SchemeWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::SchemeWindow)
{
    ui->setupUi(this);
    InitTree();
}

SchemeWindow::~SchemeWindow()
{
    delete ui;
}

void SchemeWindow::TreeItemClicked(QTreeWidgetItem *item, int col)
{
    if(item->checkState(0) == GetItemState(item, m_mapItemState))//item状态是否改变
        return;
    ChangeChildItemState(item);
    ChangeParentItemState(item);
    QList<QString> listTarget;
    GetCheckItem(ui->treeWidget, listTarget);
    qDebug() << listTarget;
}

void SchemeWindow::InitTree()
{
    connect(ui->treeWidget, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(TreeItemClicked(QTreeWidgetItem*,int)));
    ui->treeWidget->header()->setHidden(1);
    QTreeWidgetItem* topItem = new QTreeWidgetItem;
    topItem->setCheckState(0, Qt::Unchecked);
    topItem->setText(0, "目标列表");
    ui->treeWidget->addTopLevelItem(topItem);
    QTreeWidgetItem* item1 = AddTreeItem(ui->treeWidget, topItem, "目标1");
    QList<QString> listTarget;
    listTarget << "目标11" << "目标12";
    for(int i = 0; i < listTarget.count(); i++){
        AddTreeItem(ui->treeWidget, item1, listTarget.at(i));
    }

    QTreeWidgetItem* item2 = AddTreeItem(ui->treeWidget, topItem, "目标2");
    listTarget.clear();
    listTarget << "目标21" << "目标22" << "目标23";
    for(int i = 0; i < listTarget.count(); i++){
        AddTreeItem(ui->treeWidget, item2, listTarget.at(i));
    }

    QTreeWidgetItem* item3 = AddTreeItem(ui->treeWidget, topItem, "目标3");
    listTarget.clear();
    listTarget << "目标31" << "目标32" << "目标33" << "目标34";
    for(int i = 0; i < listTarget.count(); i++){
        AddTreeItem(ui->treeWidget, item3, listTarget.at(i));
    }

    ui->treeWidget->expandAll();
}

QTreeWidgetItem *SchemeWindow::AddTreeItem(QTreeWidget *pTree, QTreeWidgetItem *parentItem, QString itemTxt)
{
    QTreeWidgetItem *item = new QTreeWidgetItem(parentItem);
    item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
    item->setCheckState(0, Qt::Unchecked);
    item->setText(0, itemTxt);
    return item;
}

void SchemeWindow::GetCheckItem(QTreeWidget *pTree, QList<QString> &listTarget)
{//这个本来是为了获取选中的节点文本。后来增加了获取所有节点的状态，以备将来判断节点状态是否改变
    m_mapItemState.clear();

    QTreeWidgetItemIterator it(pTree);
    while (*it) {
        if((*it)->checkState(0) == Qt::Checked){
            QString itemTxt = (*it)->text(0);
            if(itemTxt.compare("") != 0){
                listTarget.push_back(itemTxt);
            }
        }
        m_mapItemState[(*it)] = (*it)->checkState(0);
        ++it;
    }
}
//遍历该节点下所有子节点
void SchemeWindow::ChangeChildItemState(QTreeWidgetItem *thisItem)
{
    int nChildCount = thisItem->childCount();
    if(nChildCount < 1) return;

    for(int i = 0; i < nChildCount; i++){
        thisItem->child(i)->setCheckState(0, thisItem->checkState(0));
        ChangeChildItemState(thisItem->child(i));
    }
}

void SchemeWindow::ChangeParentItemState(QTreeWidgetItem *thisItem)
{
    QTreeWidgetItem* parentItem = thisItem->parent();
    if(parentItem == NULL) return;

    int nChildCount = parentItem->childCount();
    int nSelected = 0;
    int nPartSelected = 0;
    for(int i = 0; i < nChildCount; i++){
        if(parentItem->child(i)->checkState(0) == Qt::Checked){
            nSelected++;
        }
        else if(parentItem->child(i)->checkState(0) == Qt::PartiallyChecked){
            nPartSelected++;
        }
    }
    //如果该节点有子节点被选中，但选中的节点个数小于子节点数，或者有子节点是半选中状态，则该节点为半选中
    if((nSelected > 0 && nSelected < nChildCount) || nPartSelected > 0){
        parentItem->setCheckState(0, Qt::PartiallyChecked);
    }
    else if(nSelected == nChildCount){
        parentItem->setCheckState(0, Qt::Checked);
    }
    else {
        parentItem->setCheckState(0, Qt::Unchecked);
    }
    ChangeParentItemState(parentItem);
}

Qt::CheckState SchemeWindow::GetItemState(QTreeWidgetItem *item, map<QTreeWidgetItem *, Qt::CheckState> &mapItemState)
{
    map<QTreeWidgetItem*, Qt::CheckState>::iterator iter = m_mapItemState.find(item);
    if(iter != m_mapItemState.end()){
        return iter->second;
    }
    return Qt::Unchecked;
}
